<template>
  <div id="addGoodsType">
    <el-dialog
        title="新增商品类型"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="closeBtn"
    >
      <el-form ref="formRef" :rules="formRules" :model="goodsType" label-width="80px">
        <el-form-item label="图标">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8080/goodsCategory/avatarImgUpload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="类型名称" prop="categoryName">
          <el-input v-model="goodsType.categoryName"></el-input>
        </el-form-item>
        <el-form-item label="父级类型" >
          <el-select v-model="goodsType.parentId" filterable placeholder="请选择" clearable>
            <el-option
                v-for="item in tableData"
                :key="item.id"
                :label="item.categoryName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
    <el-button @click="closeBtn">取 消</el-button>
    <el-button type="primary" @click="addGoodsTypeBtn('formRef')">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "AddGoodsType",
  data() {
    return {
      // 返回数据对象
      tableData: [],

      //图标地址
      imageUrl: "",

      //商品类型
      goodsType:{},

      dialogVisible: true,

      // 表单校验规则
      formRules: {
        categoryName: [
          { required: true, message: '类型名称不能为空', trigger: 'blur'},
        ],
      }
    }
  },
  methods: {
    //添加
    addGoodsTypeBtn(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post('/goodsCategory/addGoodsType',this.goodsType);
          this.$message.success(res);
          //自定义事件处理界面刷新
          this.$emit('query');
          this.$router.push('/goodsType');
        } else {
          this.$message.error('校验失败！')
        }
      })

    },
    //图标成功上传的回调
    handleAvatarSuccess(res, file) {
      this.imageUrl = res;
      this.goodsType.icon = this.imageUrl
    },
    //上传之前的校验
    beforeAvatarUpload(file) {
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isPNG) {
        this.$message.error('上传头像图片只能是 PNG  格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isPNG && isLt2M;
    },
    //关闭
    closeBtn() {
      this.$router.push('/goodsType');
    },
    //商品类型列表
    async queryGoodsType() {
      const { data: res } = await this.$http.post(`/goodsCategory/queryGoodsCategoryList`);
      this.tableData = res
    }
  },
  created() {
    this.queryGoodsType();
  }
}
</script >

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 78px;
  height: 78px;
  line-height: 78px;
  text-align: center;
}
.avatar {
  width: 78px;
  height: 78px;
  display: block;
}
</style>